% create a spreadsheet for OOS simple strtategy (not utility based)

clear;clc;

load P00Data;
load Market;

ALL_DLSt = cell(2,4,2); ALL_DLSSRt = cell(2,4,2);

Names = cell(4,1);
Names{1} = 'Results_Simple_M'; Names{2} = 'Results_Simple_Q'; Names{3} = 'Results_Simple_ADec'; Names{4} = 'Results_Simple_AJun';

MULT = [1200 400 100 100];
for Threshold = [50 25]
    for freq = 1:4
        load('ResultsFile',Names{freq}); Results = eval(Names{freq});
        if freq==1
            RMarket = CRSPSP_M; RFree = Rf_M; TSVAR = yyyymm; LengthT = TM;
        elseif freq==2
            RMarket = CRSPSP_Q; RMarket = RMarket(3:3:end); RFree = Rf_Q(3:3:end); TSVAR = yyyyq; LengthT = TQ;
        elseif freq==3
            RMarket = CRSPSP_A; RMarket = RMarket(12:12:end); RFree = Rf_A(12:12:end); TSVAR = yyyy; LengthT = TA;
        elseif freq==4
            RMarket = CRSPSP_A; RMarket = RMarket(6:12:end); RFree = Rf_A(6:12:end); TSVAR = yyyy; LengthT = TA;
        end

        for InvestZ = 0:1

            LongRet = NaN(46, 2, LengthT); ShortRet = NaN(46, 2, LengthT);
            LongN = NaN(46, 2, LengthT); ShortN = NaN(46, 2, LengthT);

            for i = 4 %1:46
                if freq==1 && ~strcmp(VARS(i).Freq,'Monthly')
                    continue;
                elseif freq==2 && (strcmp(VARS(i).Freq,'Annual') || strcmp(VARS(i).Freq,'Semiannual'))
                    continue;
                end

                TBeg = find(TSVAR==Results(i).SampleBeg); TEnd = find(TSVAR==Results(i).SampleEnd); OOSBeg = find(TSVAR==Results(i).OOSBeg);
                if i==13 || i==17 % these are PLS variables
                    XOOS = VARS(i).DataOOS{freq};
                elseif i==22 % svix
                    if freq==4
                        XIS = VARS(i).DataIS{3}; XOOS = VARS(i).DataOOS{3};
                    else
                        XIS = VARS(i).DataIS{freq}; XOOS = VARS(i).DataOOS{freq};
                    end
                else
                    XOOS = VARS(i).DataOOS;
                end

                if strcmp(VARS(i).Freq,'Monthly')
                    if freq==2; XOOS = XOOS(3:3:end,3:3:end); elseif (freq==3 || freq==4); XOOS = XOOS(12:12:end,12:12:end); end
                elseif strcmp(VARS(i).Freq,'Quarterly')
                    if (freq==3 || freq==4); XOOS = XOOS(4:4:end,4:4:end); end
                elseif strcmp(VARS(i).Freq,'Semiannual')
                    if (freq==3 || freq==4); XOOS = XOOS(2:2:end,2:2:end); end
                end
                if freq==4 && ~(i==13 || i==17)
                    tmp = NaN(size(XOOS)); tmp(2:end,2:end) = XOOS(1:end-1,1:end-1); XOOS = tmp; clear tmp;
                end

                for t = OOSBeg-1:TEnd-1
                    Xt = XOOS(TBeg:t,t); Xt(~isfinite(Xt)) = [];
                    Xmean = mean(Xt); Xstd = std(Xt); X50 = median(Xt); X25 = prctile(Xt,25); X75 = prctile(Xt,75);

                    % four cases: (1) InvestZ && Threshold==50; (2) InvestZ && Threshold==25; (3) ~InvestZ && Threshold==50; (4) ~InvestZ && Threshold==25
                    XBrkPt = NaN(4,1);   zscore = NaN(4,1);
                    if Results(i).ISStats.beta >= 0
                        XBrkPt(1) = X50; zscore(1) = (Xt(end) - XBrkPt(1)) / Xstd;
                        XBrkPt(2) = X25; zscore(2) = (Xt(end) - XBrkPt(2)) / Xstd;
                        XBrkPt(3) = X50; zscore(3) = 1;
                        XBrkPt(4) = X25; zscore(4) = 1;
                    else
                        XBrkPt(1) = X50; zscore(1) = (Xt(end) - XBrkPt(1)) / Xstd;
                        XBrkPt(2) = X75; zscore(2) = (XBrkPt(2) - Xt(end)) / Xstd;
                        XBrkPt(3) = X50; zscore(3) = 1;
                        XBrkPt(4) = X75; zscore(4) = 1;
                    end
                    zscore = abs(zscore);

                    if InvestZ && Threshold==50
                        XBrkPt = XBrkPt(1); zscore = zscore(1);
                    elseif InvestZ && Threshold==25
                        XBrkPt = XBrkPt(2); zscore = zscore(2);
                    elseif ~InvestZ && Threshold==50
                        XBrkPt = XBrkPt(3); zscore = zscore(3);
                    elseif ~InvestZ && Threshold==25
                        XBrkPt = XBrkPt(4); zscore = zscore(4);
                    end

                    if Results(i).ISStats.beta >= 0
                        if Xt(end) >= XBrkPt
                            LongRet(i,1,t+1) = zscore*RMarket(t+1); ShortRet(i,1,t+1) = zscore*RFree(t+1);
                            LongN(i,1,t+1) = 1;
                        else
                            LongRet(i,1,t+1) = zscore*RFree(t+1); ShortRet(i,1,t+1) = zscore*RMarket(t+1);
                            ShortN(i,1,t+1) = 1;
                        end
                    else
                        if Xt(end) <= XBrkPt
                            LongRet(i,1,t+1) = zscore*RMarket(t+1); ShortRet(i,1,t+1) = zscore*RFree(t+1);
                            LongN(i,1,t+1) = 1;
                        else
                            LongRet(i,1,t+1) = zscore*RFree(t+1); ShortRet(i,1,t+1) = zscore*RMarket(t+1);
                            ShortN(i,1,t+1) = 1;
                        end
                    end
                    LongRet(i,2,t+1) = zscore*RMarket(t+1); ShortRet(i,2,t+1) = zscore*RFree(t+1);
                    LongN(i,2,t+1) = 1;
                end
            end

            %LongRet = LongRet*MULT(freq); ShortRet = ShortRet*MULT(freq);

            LongShortRet = LongRet - ShortRet;
            DLongShortRet = LongShortRet(:,1,:) - LongShortRet(:,2,:);

            L = mean(LongRet,3,'omitnan'); S = mean(ShortRet,3,'omitnan'); LS = mean(LongShortRet,3,'omitnan');
            LN = sum(LongN,3,'omitnan'); SN = sum(ShortN,3,'omitnan');

            DLSm = mean(DLongShortRet,3,'omitnan');
            DLSs = std(DLongShortRet,[],3,'omitnan');
            DLSn = squeeze(sum(isfinite(DLongShortRet),3));
            DLSt = sqrt(DLSn).*DLSm./DLSs;

            DLSSRm = DLSm./DLSs;
            DLSSRs = sqrt((DLSSRm.^2 + 2)./(2*DLSn));
            DLSSRt = DLSSRm./DLSSRs;

            L = L*MULT(freq); S = S*MULT(freq); LS = LS*MULT(freq); DLSm = DLSm*MULT(freq); DLSSRm = DLSSRm*sqrt(MULT(freq))/10;

            fprintf('Threshold = %d, freq = %d, InvestZ = %d\n', Threshold, freq, InvestZ);
            for i = 1:46
                if freq==1 && ~strcmp(VARS(i).Freq,'Monthly')
                    continue;
                elseif freq==2 && (strcmp(VARS(i).Freq,'Annual') || strcmp(VARS(i).Freq,'Semiannual'))
                    continue;
                end

                % 1-sided tests
                fprintf('%0.2f ',[i VARS(i).PaperNum Results(i).SampleBeg Results(i).SampleEnd Results(i).OOSBeg L(i,1) S(i,1) LS(i,1) LN(i,1) SN(i,1) NaN L(i,2) S(i,2) LS(i,2) NaN DLSm(i,1)]);
                if DLSt(i,1)>=norminv(0.99); fprintf('*** '); elseif DLSt(i,1)>=norminv(0.95); fprintf('** '); elseif DLSt(i,1)>=norminv(0.90); fprintf('* '); else; fprintf('NS '); end

                fprintf('%0.2f ',DLSSRm(i,1));
                if DLSSRt(i,1)>=norminv(0.99); fprintf('***\n'); elseif DLSSRt(i,1)>=norminv(0.95); fprintf('**\n'); elseif DLSSRt(i,1)>=norminv(0.90); fprintf('*\n'); else; fprintf('NS\n'); end
            end
            disp(' ');

            ThresholdCtr = Threshold/25;
            ALL_DLSt{ThresholdCtr, freq, InvestZ+1} = DLSt;
            ALL_DLSSRt{ThresholdCtr, freq, InvestZ+1} = DLSSRt;
        end %end InvestZ
    end % end freq
end % end Threshold
